﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.ComponentModel;
    using System.Xml;

    [ScriptType("SP.BasePermissions", ValueObject=true, ServerTypeId="{db780e5a-6bc6-41ad-8e64-9dfa761afb6d}")]
    public sealed class BasePermissions : ClientValueObject
    {
        private uint m_high;
        private uint m_low;

        public void Clear(PermissionKind perm)
        {
            int num = (int) perm;
            num--;
            uint num2 = 1;
            if ((num >= 0) && (num < 0x20))
            {
                num2 = num2 << num;
                num2 = ~num2;
                this.m_low &= num2;
            }
            else if ((num >= 0x20) && (num < 0x40))
            {
                num2 = num2 << (num - 0x20);
                num2 = ~num2;
                this.m_high &= num2;
            }
        }

        public void ClearAll()
        {
            this.m_high = 0;
            this.m_low = 0;
        }

        public override bool Equals(object obj)
        {
            BasePermissions permissions = obj as BasePermissions;
            if (permissions == null)
            {
                return false;
            }
            return ((permissions.m_high == this.m_high) && (permissions.m_low == this.m_low));
        }

        public override int GetHashCode()
        {
            return (this.m_high.GetHashCode() + this.m_low.GetHashCode());
        }

        public bool Has(PermissionKind perm)
        {
            if (perm == PermissionKind.EmptyMask)
            {
                return true;
            }
            if (perm == PermissionKind.FullMask)
            {
                return (((this.m_high & 0x7fff) == 0x7fff) && (this.m_low == 0xffff));
            }
            int num = (int) perm;
            num--;
            uint num2 = 1;
            if ((num >= 0) && (num < 0x20))
            {
                num2 = num2 << num;
                return (0 != (this.m_low & num2));
            }
            if ((num >= 0x20) && (num < 0x40))
            {
                num2 = num2 << (num - 0x20);
                return (0 != (this.m_high & num2));
            }
            return false;
        }

        public bool HasPermissions(uint high, uint low)
        {
            return (((this.m_high & high) == high) && ((this.m_low & low) == low));
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = false;
            string str = peekedName;
            if (str != null)
            {
                if (!(str == "High"))
                {
                    if (str == "Low")
                    {
                        flag = true;
                        reader.ReadName();
                        this.m_low = reader.ReadUInt32();
                    }
                    return flag;
                }
                flag = true;
                reader.ReadName();
                this.m_high = reader.ReadUInt32();
            }
            return flag;
        }

        public void Set(PermissionKind perm)
        {
            if (perm == PermissionKind.FullMask)
            {
                this.m_low = 0xffff;
                this.m_high = 0x7fff;
            }
            else if (perm == PermissionKind.EmptyMask)
            {
                this.m_low = 0;
                this.m_high = 0;
            }
            else
            {
                int num = (int) perm;
                num--;
                uint num2 = 1;
                if ((num >= 0) && (num < 0x20))
                {
                    num2 = num2 << num;
                    this.m_low |= num2;
                }
                else if ((num >= 0x20) && (num < 0x40))
                {
                    num2 = num2 << (num - 0x20);
                    this.m_high |= num2;
                }
            }
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public override void WriteToXml(XmlWriter writer, SerializationContext serializationContext)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (serializationContext == null)
            {
                throw new ArgumentNullException("serializationContext");
            }
            writer.WriteStartElement("Property");
            writer.WriteAttributeString("Name", "High");
            DataConvert.WriteValueToXmlElement(writer, this.High, serializationContext);
            writer.WriteEndElement();
            writer.WriteStartElement("Property");
            writer.WriteAttributeString("Name", "Low");
            DataConvert.WriteValueToXmlElement(writer, this.Low, serializationContext);
            writer.WriteEndElement();
        }

        [Remote]
        internal uint High
        {
            get
            {
                return this.m_high;
            }
            set
            {
                this.m_high = value;
            }
        }

        [Remote]
        internal uint Low
        {
            get
            {
                return this.m_low;
            }
            set
            {
                this.m_low = value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public override string TypeId
        {
            get
            {
                return "{db780e5a-6bc6-41ad-8e64-9dfa761afb6d}";
            }
        }
    }
}

